<template>
  <div class="home-container">
    <el-container class="box">
      <el-header>
        <div class="header-left">
          <img src="@/assets/heima.png"
               alt="" />
          <span class="title">电商后台管理系统</span>
        </div>
        <div>
          <el-button @click="logout"
                     type="info">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="collapse ? '70px' : '200px'">
          <div class="menuBtn"
               @click="collapse = !collapse">| | |</div>
          <el-menu :default-active="defaultActive"
                   class="el-menu-vertical-demo"
                   background-color="#373d41"
                   :unique-opened="true"
                   :collapse="collapse"
                   :collapse-transition="false"
                   router
                   text-color="#fff"
                   active-text-color="#409eff">
            <el-submenu :index="item1.path + ''"
                        v-for="(item1, index1) in menusList"
                        :key="index1">
              <template slot="title">
                <!-- <i class="el-icon-location"></i> -->
                <i style="margin-right:8px;"
                   :class="iconObj[item1.id]"></i>
                <span>{{ item1.authName }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item @click.native="itemHandle(item2.path)"
                              :index="item2.path + ''"
                              v-for="(item2, index2) in item1.children"
                              :key="index2">
                  <i class="el-icon-menu"></i>{{ item2.authName }}
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 二级路由 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { menusApi } from '@/api/index'
export default {
  name: '',
  props: {},
  data () {
    return {
      menusList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      defaultActive: localStorage.getItem('defaultActive'),
      collapse: false //  false =>展开   true =>收起
    }
  },
  methods: {
    logout () {
      this.$confirm('是否确认退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // this.$message({
          //   type: 'success',
          //   message: '删除成功!'
          // })
          // 这里表示的是点击了 确认按钮
          // 点击确认按钮 === 清空token [本地缓存]
          localStorage.clear()
          // 跳转会login
          this.$router.push('/login')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    },
    // handleOpen (key, keyPath) {
    //   console.log(key, keyPath)
    // },
    // handleClose (key, keyPath) {
    //   console.log(key, keyPath)
    // },
    async getMenus () {
      const res = await menusApi()
      console.log(res)
      this.menusList = res.data.data
    },
    itemHandle (path) {
      console.log(path)
      localStorage.setItem('defaultActive', path)
      this.defaultActive = path
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getMenus()
  },
  mounted () { },
  components: {}
}
</script>

<style scoped lang="less">
.el-header,
.el-footer {
  background-color: #373d41;
  color: #333;
  text-align: center;
  height: 100%;
  line-height: 60px;
}

.el-aside {
  background-color: #373d41;
  color: #333;
  text-align: left;
  .menuBtn {
    color: #fff;
    text-align: center;
    font-size: 13px;
    padding: 2px 0;
    background-color: #4a5064;
    cursor: pointer;
  }
  .el-menu {
    border: none;
    /deep/ul.el-menu {
      text-align: center;
      .el-menu-item-group__title {
        padding: 0;
      }
    }
  }
  -moz-user-select: none; /*火狐*/
  -webkit-user-select: none; /*webkit浏览器*/
  -ms-user-select: none; /*IE10*/
  -khtml-user-select: none; /*早期浏览器*/
  user-select: none;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  // text-align: center;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.home-container .el-header {
  display: flex;
  justify-content: space-between;
}
.header-left {
  display: flex;
  align-items: center;
}
.header-left img {
  margin-right: 15px;
}
.el-header .title {
  font-size: 22px;
  color: #fff;
}
.home-container {
  height: 100%;
  .box {
    height: 100%;
  }
}
</style>
